package com.example.mqltest.service.serviceImp;


import com.example.mqltest.Dto.DeleteStudentsDTO;
import com.example.mqltest.dao.*;
import com.example.mqltest.entity.*;
import com.example.mqltest.service.AccountService;
import com.example.mqltest.service.CourseService;
import com.example.mqltest.service.StudentService;
import com.example.mqltest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class TeacherServiceImp  implements TeacherService {
    @Autowired
    TeacherDao teacherDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    CourseDao courseDao;

    @Autowired
    private  WorkDao workDao;


    @Override
    public Result register(Teacher teacher) {
        if(studentDao.getStudentById( teacher.getTeacherId() )!=null || teacherDao.getTeacherById( teacher.getTeacherId() )!=null){
            return new Result(400,"注册失败，原因是ID已存在","");
        }
        if(studentDao.getStudentByPhoneNum( teacher.getPhoneNum() )!=null || teacherDao.getTeacherByPhoneNum( teacher.getPhoneNum() )!=null){
            return new Result(400,"注册失败，原因是手机号邮箱已存在","");
        }
        try {
            teacherDao.register(teacher);
            return new Result(200,"注册成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙,注册失败","");
        }
    }

    @Override
    public Result deleteStudent(String code, String studentId) {
        try {
            teacherDao.deleteStudent(code,studentId);
            return new Result(200,"删除成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，删除失败","");
        }
    }

    @Override
    public Result deleteCourse(String code) {
        try {
            courseDao.deleteCourse(code);
            return new Result(200,"删除成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，删除失败","");
        }
    }

    @Override
    public Result addWork(Work work) {
        try {
            teacherDao.addWork(work);
            List<Student> studentList=studentDao.getStudentByCode( work.getCode() );
            for (Student student:studentList
                 ) {
                teacherDao.addWorkList( work.getId(),student.getStudentId() );
            }
            return new Result(200,"增加课程成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，删除失败","");
        }
    }

    @Override
    public Result deleteCheckedStudent(DeleteStudentsDTO deleteStudentsDTO) {
        try {
            courseDao.deleteCheckedStudent(deleteStudentsDTO);
            return new Result(200,"批量删除成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，批量删除失败","");
        }
    }

    //批改作业
    @Override
    public Result correctWork(int workId, String studentId,int grade) {
        try {
            workDao.correctWork(workId,studentId,"已批改",grade);
            return new Result(200,"批阅成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙,批阅失败","");
        }
    }

    @Override
    public Result joinCourse(String code, String teacherId) {
        if(teacherDao.IsJoinCourse( code,teacherId )!=null){
            return new Result(404,"你已经加入该课程","");
        }
        try {
            teacherDao.joinCourse(code,teacherId);
            return new Result(200,"加入成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，加入失败","");
        }
    }

}
